package com.example.business.user.controller;

import com.example.business.user.dto.UserDTO;
import com.example.business.user.service.UserService;
import com.example.common.dto.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/users")
@Api(tags = "用户管理")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @GetMapping
    @ApiOperation("获取用户列表")
    public BaseResponse<List<UserDTO>> listUsers() {
        List<UserDTO> users = userService.listUsers();
        return new BaseResponse<>(200, "获取成功", users);
    }
    
    @GetMapping("/{id}")
    @ApiOperation("根据ID获取用户")
    public BaseResponse<UserDTO> getUserById(@PathVariable Long id) {
        UserDTO user = userService.getUserById(id);
        return new BaseResponse<>(200, "获取成功", user);
    }
    
    @PostMapping
    @ApiOperation("创建用户")
    public BaseResponse<UserDTO> createUser(@Valid @RequestBody UserDTO userDTO) {
        UserDTO user = userService.createUser(userDTO);
        return new BaseResponse<>(200, "创建成功", user);
    }
    
    @PutMapping("/{id}")
    @ApiOperation("更新用户")
    public BaseResponse<UserDTO> updateUser(@PathVariable Long id, @Valid @RequestBody UserDTO userDTO) {
        UserDTO user = userService.updateUser(id, userDTO);
        return new BaseResponse<>(200, "更新成功", user);
    }
    
    @DeleteMapping("/{id}")
    @ApiOperation("删除用户")
    public BaseResponse<Void> deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return new BaseResponse<>(200, "删除成功", null);
    }
} 